﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.IServices.Tms;
using PeiYue.Model.Models.Tms;
using PeiYue.Common.HttpContextUser;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using PeiYue.Api.Controllers.BASE;
using PeiYue.Model.Dto;
using System.Collections.Generic;
using System.Linq;
using PeiYue.IServices.Sys;

namespace PeiYue.Api.Controllers.Tms
{
    /// <summary>
    /// 线路管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class RouterController : BaseController<RouterDao>
    {
        readonly IRouterServices _routerServices;
        readonly IDicHeaderService _dicHeaderService;
        readonly IUser _user;
        readonly IMapper _mapper;
        public RouterController(IRouterServices routerServices, IDicHeaderService dicHeaderService, IUser user, IMapper mapper):base(routerServices,user)
        {
            _routerServices = routerServices;
            _dicHeaderService = dicHeaderService;
            _user = user;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取线路详情
        /// </summary>
        /// <param name="id">线路id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<SearchRouterResponseDto>> Get(long id)
        {
            var model = await _routerServices.QueryById(id);
            var dto = _mapper.Map<SearchRouterResponseDto>(model);
            return new MessageModel<SearchRouterResponseDto>()
            {
                msg = "获取成功",
                success = true,
                response = dto
            };
        }
        /// <summary>
        /// 获取线路分页列表
        /// </summary>
        /// <param name="requestDto">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<SearchRouterResponseDto>>> GetList([FromQuery]SearchRouterRequestDto requestDto)
        {
            var data = await _routerServices.GetPage(requestDto);
            return new MessageModel<PageModel<SearchRouterResponseDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }
        /// <summary>
        /// 新增线路
        /// </summary>
        /// <param name="routerDto">新增线路参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Add([FromBody] UpdateRouterDto routerDto)
        {
            var data = new MessageModel<string>();
            var list = await _routerServices.Query(t => t.names == routerDto.names && t.status == (int)CommonEnum.Status.Enable);
            if (list.Count > 0)
            {
                data.msg = $"[{routerDto.names}]线路名称已经存在";
                data.status = 500;
                data.success = false;
                return data;
            }
            list = await _routerServices.Query(t => t.codec == routerDto.codec && t.status == (int)CommonEnum.Status.Enable);
            if (list.Count > 0)
            {
                data.msg = $"[{routerDto.codec}]线路编码已经存在";
                data.status = 500;
                data.success = false;
                return data;
            }
            var routerDao = _mapper.Map<RouterDao>(routerDto);
            routerDao.id = await _routerServices.GenId();
            routerDao.create_user = _user.ID;
            routerDao.update_user = _user.ID;
            await _routerServices.Add(routerDao);
            if (routerDao.id > 0)
            {
                data.success = true;
                data.msg = "添加成功";
            }
            return data;
        }
        /// <summary>
        /// 修改线路
        /// </summary>
        /// <param name="routerDto">修改线路参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Update([FromBody] UpdateRouterDto routerDto)
        {
            var data = new MessageModel<string>();
            var routerDao = _mapper.Map<RouterDao>(routerDto);
            if (routerDao != null && routerDao.id > 0)
            {
                var list = await _routerServices.Query(t =>t.id!=routerDto.id && t.names == routerDto.names && t.status == (int)CommonEnum.Status.Enable);
                if (list.Count > 0)
                {
                    data.msg = $"[{routerDto.names}]线路名称已经存在";
                    data.status = 500;
                    data.success = false;
                    return data;
                }
                list = await _routerServices.Query(t => t.id != routerDto.id && t.codec == routerDto.codec && t.status == (int)CommonEnum.Status.Enable);
                if (list.Count > 0)
                {
                    data.msg = $"[{routerDto.codec}]线路编码已经存在";
                    data.status = 500;
                    data.success = false;
                    return data;
                }
                routerDao.update_user = _user.ID;
                data.success = await _routerServices.Update(routerDao, null, new List<string> { "status", "create_time", "create_user" });
                if (data.success)
                {
                    data.msg = "更新成功";
                    data.response = routerDao?.id.ObjToString();
                }
            }
            return data;
        }
        /// <summary>
        /// 获取线路下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<MessageModel<List<SelectItemDto>>> GetSelectItem()
        {
            var data = await _routerServices.Query(p => p.status == 1);
            var result = data.Select(p => new SelectItemDto { Id = p.id, Name = p.names }).ToList();
            return new MessageModel<List<SelectItemDto>>()
            {
                msg = "获取成功",
                success = true,
                response = result
            };
        }
        /// <summary>
        /// 获取线路类型下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<MessageModel<List<SelectItemDto>>> GetRouterType()
        {
            var ss = await _dicHeaderService.GetDicHeader("router_type");
            var data = (await _dicHeaderService.GetDicHeader("router_type")).ToSelectList();
            return new MessageModel<List<SelectItemDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }
    }
}